package cn.com.self.apis.escrow.service.impl;

import cn.com.self.apis.escrow.mapper.EscrowInfoMapper;
import cn.com.self.apis.files.entity.Files;
import cn.com.self.apis.member.entity.UFileRelation;
import cn.com.self.apis.member.entity.UMemberInfo;
import cn.com.self.apis.member.mapper.MemberInfoMapper;
import cn.com.self.apis.member.mapper.ReportFileMapper;
import cn.com.self.apis.portal.util.ConstantUtils;
import cn.com.self.apis.util.CopyUtils;
import cn.com.selfsoft.smartcity.commons.http.DataItem;
import cn.com.selfsoft.smartcity.commons.http.ResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import cn.com.self.apis.escrow.entity.EscrowInfo;
import cn.com.self.apis.escrow.service.IEscrowInfoService;
import cn.com.selfsoft.smartcity.core.service.BaseServiceImpl;

import java.util.ArrayList;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liangwt
 * @since 2020-05-28
 */
@Service
public class EscrowInfoServiceImpl extends BaseServiceImpl<EscrowInfo, String> implements IEscrowInfoService {

    @Autowired
    private EscrowInfoMapper escrowInfoMapper;
    @Autowired
    private ReportFileMapper reportFileMapper;
    @Autowired
    private MemberInfoMapper memberInfoMapper;
    @Override
    public EscrowInfo loadEscrowInfoById(String id) {

        EscrowInfo escrowInfo = escrowInfoMapper.loadEscrowInfoById(id);
        if(escrowInfo == null){
            return null;
        }
        ArrayList<Files> demandFiles = escrowInfoMapper.queryEscrowInfoFiles(id, ConstantUtils.FILE_TYPE_HETONG);
        ArrayList<Files> contractFiles = escrowInfoMapper.queryEscrowInfoFiles(id,ConstantUtils.FILE_TYPE_XUQIU);
        escrowInfo.setDemandFiles(demandFiles);
        escrowInfo.setContractFiles(contractFiles);
        return escrowInfo;

    }

    @Override
    public ResponseEntity createEscrowInfo(EscrowInfo entity) {
        UMemberInfo uMemberInfo = memberInfoMapper.selectById(entity.getInitiatorId());
        if(uMemberInfo == null){
            return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(404,"该托管发起人不存在！"));
        }
        entity.setCreateDate(new Date());
        entity.setUpdateDate(new Date());
        entity.setProgress("0%");
        EscrowInfo escrowInfo = this.create(entity);
        ArrayList<Files> demandFiles = entity.getDemandFiles();
        if(demandFiles != null){
            for(Files file : demandFiles){
                UFileRelation uFileRelation = new UFileRelation();
                uFileRelation.setFileId(file.getId());
                uFileRelation.setObjId(escrowInfo.getId());
                uFileRelation.setType(ConstantUtils.FILE_TYPE_HETONG);
                reportFileMapper.insert(uFileRelation);
            }
        }
        ArrayList<Files> contractFiles = entity.getContractFiles();
        if(contractFiles != null){
            for(Files contractFile : contractFiles){
                UFileRelation uFileRelation = new UFileRelation();
                uFileRelation.setFileId(contractFile.getId());
                uFileRelation.setObjId(escrowInfo.getId());
                uFileRelation.setType(ConstantUtils.FILE_TYPE_XUQIU);
                reportFileMapper.insert(uFileRelation);
            }
        }
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataItem(escrowInfo)));
    }

    @Override
    public ResponseEntity updateEscrowInfo(String id, EscrowInfo entity) {
        UMemberInfo uMemberInfo = memberInfoMapper.selectById(entity.getInitiatorId());
        if(uMemberInfo == null){
            return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(404,"该托管发起人不存在！"));
        }
        escrowInfoMapper.deleteUFile(id,ConstantUtils.FILE_TYPE_HETONG);
        escrowInfoMapper.deleteUFile(id,ConstantUtils.FILE_TYPE_XUQIU);
        EscrowInfo escrowInfo = this.load(id);
        CopyUtils.copyProperties(entity, escrowInfo);
        escrowInfo.setUpdateDate(new Date());
        EscrowInfo update = this.update(escrowInfo);
        ArrayList<Files> demandFiles = entity.getDemandFiles();
        if(demandFiles != null){
            for(Files file : demandFiles){
                UFileRelation uFileRelation = new UFileRelation();
                uFileRelation.setFileId(file.getId());
                uFileRelation.setObjId(escrowInfo.getId());
                uFileRelation.setType(ConstantUtils.FILE_TYPE_HETONG);
                reportFileMapper.insert(uFileRelation);
            }
        }
        ArrayList<Files> contractFiles = entity.getContractFiles();
        if(contractFiles != null){
            for(Files contractFile : contractFiles){
                UFileRelation uFileRelation = new UFileRelation();
                uFileRelation.setFileId(contractFile.getId());
                uFileRelation.setObjId(escrowInfo.getId());
                uFileRelation.setType(ConstantUtils.FILE_TYPE_XUQIU);
                reportFileMapper.insert(uFileRelation);
            }
        }
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataItem(update)));
    }

    @Override
    public void deleteUFile(String id) {
        escrowInfoMapper.deleteUFile(id,ConstantUtils.FILE_TYPE_HETONG);
        escrowInfoMapper.deleteUFile(id,ConstantUtils.FILE_TYPE_XUQIU);
    }
}
